package com.njlime.core.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.helper.PageBean;
import com.njlime.core.sdk.wechat.UrlHeader;
import com.njlime.core.utils.StringUtil;
import com.njlime.core.web.AppWxInterceptor;
import com.njlime.member.entiies.TMember;
import com.njlime.sys.entities.UnitWxConfig;
import com.njlime.sys.service.UnitService;
import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Controller基类
 * Created by Dean on 2017-7-18.
 */
@Controller
public class BaseAppController {

    protected Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private HttpServletRequest request;
    @Resource
    protected UnitService unitService;

    /**
     * 清空session用户数据
     */
    public void clearSession(){
        request.getSession().removeAttribute(AppWxInterceptor.WX_USER_ID);
        request.getSession().removeAttribute(AppWxInterceptor.WX_USER_NAME);
        request.getSession().removeAttribute(AppWxInterceptor.WX_USER_PIC);
    }

    /**
     * 添加session用户数据
     * @param member
     */
    public void addSession(TMember member){
        request.getSession().setAttribute(AppWxInterceptor.WX_USER_OPENID, member.getOpenid());
        request.getSession().setAttribute(AppWxInterceptor.WX_USER_ID, member.getId());
        request.getSession().setAttribute(AppWxInterceptor.WX_USER_NAME, member.getName());
        request.getSession().setAttribute(AppWxInterceptor.WX_USER_PIC, member.getHead_url());
        request.getSession().setAttribute(AppWxInterceptor.WX_USER_UNIT_ID, member.getUnit_id());
        request.getSession().setAttribute(AppWxInterceptor.WX_USER_ROOT_UNIT_ID, unitService.getRootUnit(member.getUnit_id()));
    }

    /**
     * 返回列表
     *
     * @return
     */
    public String toJsonListData(PageBean pb) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resCode", ExceptionState.SUCCESS);
        JSONObject list = new JSONObject();
        list.put("totalCount", pb.getTotalCount());
        list.put("totalPage", pb.getTotalPage());
        list.put("pageSize", pb.getPageSize());
        if (pb.getMapList() != null) {
            list.put("pageCount", pb.getMapList().size());
        } else {
            list.put("pageCount", 0);
        }
        list.put("curPage", pb.getCurPage());
        list.put("list", pb.getMapList());
        jsonObject.put("resultList", list);
        return JSON2String(jsonObject);
    }

    /**
     * 返回对象
     *
     * @param list
     * @return
     */
    public String toJsonListData(List<Map<String,Object>> list) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resCode", ExceptionState.SUCCESS);
        jsonObject.put("resultList", list);
        return JSON2String(jsonObject);
    }

    /**
     * 返回对象
     *
     * @param list
     * @return
     */
    public String toJsonListData(Object list) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resCode", ExceptionState.SUCCESS);
        jsonObject.put("resultList", list);
        return JSON2String(jsonObject);
    }

    /**
     * 返回成功
     *
     * @return
     */
    public String toJsonSuccessData() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resCode", ExceptionState.SUCCESS);
        return JSON2String(jsonObject);
    }

    /**
     * 返回成功
     *
     * @param object 需要返回的数据
     * @return
     */
    public String toJsonSuccessData(Object object) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resCode", ExceptionState.SUCCESS);
        jsonObject.put("resultList", object);
        return JSON2String(jsonObject);
    }

    /**
     * 返回失败结果，并带上message及结果
     *
     * @param errorCode
     * @param errorMessage
     * @param resultObj
     * @return
     */
    public String toJsonErrorData(Integer errorCode, String errorMessage, Object resultObj) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resCode", errorCode);
        jsonObject.put("message", errorMessage);
        jsonObject.put("resultList", resultObj);
        return JSON2String(jsonObject);
    }

    /**
     * 返回失败结果
     *
     * @param errorCode    错误码
     * @param errorMessage 错误信息
     * @return
     */
    public String toJsonErrorData(Integer errorCode, String errorMessage) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resCode", errorCode);
        jsonObject.put("message", errorMessage);
        return JSON2String(jsonObject);
    }

    /**
     * 返回json字符串
     * @param jsonObject
     * @return
     */
    private String JSON2String(JSONObject jsonObject){
        jsonObject.put("permission", request.getAttribute("permission"));
        return jsonObject.toJSONString();
    }

}
